package com.jy.wms.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jy.wms.common.base.*;
import com.jy.wms.common.annotions.BaseServiceDescribe;
import com.jy.wms.dao.InventoryStatisticsMapper;
import com.jy.wms.pojo.*;
import io.swagger.models.auth.In;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.sql.Date;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class InventoryStatisticsService extends BaseService<InventoryStatisticsMapper,InventoryStatistics> {
    @Autowired
    private SysDictionaryService sysDictionaryService;
    /**
     * 库存统计报表
     */
    @BaseServiceDescribe(moduleName = "报表",btnName = "库存统计报表")
    public Page<InventoryStatistics> stockCount(InventoryStatistics entity)throws ServiceException{
        PageHelper.startPage(entity.getPage(),entity.getRows());
       Page<InventoryStatistics> list= (Page<InventoryStatistics>)dao.stockCount(entity);
       String hpbh="";
       int count=1;
       for(InventoryStatistics statistics:list.getResult()){
           String zje=dao.zje(statistics.getGoodsId(),statistics.getHppc());
           Double f=Double.parseDouble(zje)*Double.parseDouble(statistics.getJe());
           statistics.setZje(f.toString());
           statistics.setZsl(dao.zsl(statistics.getGoodsId(),statistics.getHppc()));
           statistics.setJhqsl(dao.jhqsl(statistics.getGoodsId(),statistics.getHppc()));
           statistics.setCcqsl(dao.ccqsl(statistics.getGoodsId(),statistics.getHppc()));
           if(hpbh.equals(statistics.getGoodsBh())){
               statistics.setHpdj(count+1);
               count++;
           }else{
               statistics.setHpdj(1);
               hpbh=statistics.getGoodsBh();
               count=1;
           }
       }
       return list;
    }
    /**
     * 导出库存统计报表
     */
    @BaseServiceDescribe(moduleName = "导出报表",btnName = "导出库存统计报表")
    public List<InventoryStatistics> stockCountExport(InventoryStatistics entity){
        List<InventoryStatistics> list = dao.stockCount(entity);
        String hpbh="";
        int count=1;
        for(InventoryStatistics statistics:list){
            String zje=dao.zje(statistics.getGoodsId(),statistics.getHppc());
            System.out.println(Double.parseDouble(zje));
            System.out.println(Double.parseDouble(statistics.getJe()));
            Double f=Double.parseDouble(zje)*Double.parseDouble(statistics.getJe());
            statistics.setZje(f.toString());
            statistics.setZsl(dao.zsl(statistics.getGoodsId(),statistics.getHppc()));
            statistics.setJhqsl(dao.jhqsl(statistics.getGoodsId(),statistics.getHppc()));
            statistics.setCcqsl(dao.ccqsl(statistics.getGoodsId(),statistics.getHppc()));
            if(hpbh.equals(statistics.getGoodsBh())){
                statistics.setHpdj(count+1);
                count++;
            }else{
                statistics.setHpdj(1);
                hpbh=statistics.getGoodsBh();
                count=1;
            }
        }
        return list;
    }
    /**
     * 发运情况分析_Jane
     */
    @BaseServiceDescribe(moduleName = "报表",btnName = "发运情况分析")
    public Page<ShipmentOrderFx> shipmentOrderFx(ShipmentOrderFx fx)throws ServiceException{
        PageHelper.startPage(fx.getPage(),fx.getRows());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(fx.getCjsj_start()!=null && fx.getCjsj_start()!=""){
            Date date =new Date(new Long(fx.getCjsj_start()));
            fx.setCjsj_start(sdf.format(date));
        }
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar c=Calendar.getInstance();
            if (fx.getCjsj_end() != null && fx.getCjsj_end() != "") {
                c.setTime(new java.util.Date(Long.valueOf(fx.getCjsj_end())));
                c.set(Calendar.HOUR_OF_DAY, 23);
                c.set(Calendar.MINUTE, 59);
                c.set(Calendar.SECOND, 59);
                fx.setCjsj_end(format.format(c.getTime()));
            }

        Page<ShipmentOrderFx> list= (Page<ShipmentOrderFx>)dao.shipmentOrderFxa(fx);
       for(ShipmentOrderFx fxs:list.getResult()){
           Double yf=yfjs(fxs.getGoodsId(),fx);
           BigDecimal b = new BigDecimal(yf);
           Double f2 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
               fxs.setYf(f2.toString());

            if("0.0".equals(yf.toString())){

                fxs.setZb("0");
            }else{
                if("0.00".equals(fxs.getXse())) {
                    fxs.setZb("0");
                }else {
                    if("0.00".equals(fxs.getXse())) {
                        fxs.setZb("0");
                    }else {
                        String f=fxs.getXse().replaceAll(",","");
                        BigDecimal f1 = new BigDecimal((yf) / Double.parseDouble(f)).setScale(2, BigDecimal.ROUND_HALF_UP);
                        fxs.setZb(f1.toString());
                    }
                }
            }
       }
        return list;
    }
    /**
     * 发运情况分析_Jane
     */
    @BaseServiceDescribe(moduleName = "导出报表",btnName = "导出发运情况分析")
    public List<ShipmentOrderFx> shipmentOrderFxExport(ShipmentOrderFx fx){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if(fx.getCjsj_start()!=null){
            Date date=new Date(new Long(fx.getCjsj_start()));
            fx.setCjsj_start(sdf.format(date));
        }
        Calendar c=Calendar.getInstance();
        if (fx.getCjsj_end() != null && fx.getCjsj_end() != "") {
            c.setTime(new java.util.Date(Long.valueOf(fx.getCjsj_end())));
            c.set(Calendar.HOUR_OF_DAY, 23);
            c.set(Calendar.MINUTE, 59);
            c.set(Calendar.SECOND, 59);
            fx.setCjsj_end(sdf.format(c.getTime()));
        }
        List<ShipmentOrderFx> list= dao.shipmentOrderFxa(fx);
        for(ShipmentOrderFx fxs:list){
            Double yf=yfjs(fxs.getGoodsId(),fx);
            BigDecimal b = new BigDecimal(yf);
            Double f2 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            fxs.setYf(f2.toString());
            if("0.0".equals(yf.toString())){
                fxs.setZb("0");
            }else{
                if("0.00".equals(fxs.getXse())) {
                    fxs.setZb("0");
                }else {
                    String f = fxs.getXse().replaceAll(",", "");
                    BigDecimal f1 = new BigDecimal(Double.parseDouble(yf.toString()) / Double.parseDouble(f)).setScale(2, BigDecimal.ROUND_HALF_UP);
                    fxs.setZb(f1.toString());
                }
            }
        }
        return list;
    }
    /**
     * 品项数分析_Jane
     */
    @BaseServiceDescribe(moduleName = "报表",btnName = "品项数分析表头")
    public JSONObject seachPxsfxTitle(ShipmentOrderFx fx)throws ServiceException{
        JSONObject object=new JSONObject();
        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();

        if(fx.getZlids()!=null&&fx.getZlids().size()>0){
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("search","false");
            map.put("minWidth","0px");
            map.put("cRemarks","");
            map.put("prop","zlmc");
            map.put("label","货品种类");
            list.add(map);
        }
        if(fx.getHpids()!=null&&fx.getHpids().size()>0){
            Map<String,Object> map1=new HashMap<String,Object>();
            map1.put("search","false");
            map1.put("minWidth","0px");
            map1.put("cRemarks","");
            map1.put("prop","hpmc");
            map1.put("label","货品名称");
            list.add(map1);
        }
        if(fx.getChanneIds()!=null&&fx.getChanneIds().size()>0){
            Map<String,Object> map2=new HashMap<String,Object>();
            map2.put("search","false");
            map2.put("minWidth","0px");
            map2.put("cRemarks","");
            map2.put("prop","qdmc");
            map2.put("label","销售系统");
            list.add(map2);
        }
        if((fx.getChanneIds()==null||fx.getChanneIds().size()==0)&&(fx.getHpids()==null||fx.getHpids().size()==0)&&(fx.getZlids()==null||fx.getZlids().size()==0)){
            throw new ServiceException("必须选择一个条件才可查询数据");
        }
        Map<String,Object> map3=new HashMap<String,Object>();
        map3.put("search","false");
        map3.put("minWidth","0px");
        map3.put("cRemarks","");
        map3.put("prop","xssl");
        map3.put("label","销售数量");
        list.add(map3);
        Map<String,Object> map4=new HashMap<String,Object>();
        map4.put("search","false");
        map4.put("minWidth","0px");
        map4.put("cRemarks","");
        map4.put("prop","je");
        map4.put("label","单价");
        list.add(map4);
        Map<String,Object> map5=new HashMap<String,Object>();
        map5.put("search","false");
        map5.put("minWidth","0px");
        map5.put("cRemarks","");
        map5.put("prop","xse");
        map5.put("label","销售额");
        list.add(map5);
        Map<String,Object> map6=new HashMap<String,Object>();
        map6.put("search","false");
        map6.put("minWidth","0px");
        map6.put("cRemarks","");
        map6.put("prop","yf");
        map6.put("label","运费");
        list.add(map6);
        Map<String,Object> map7=new HashMap<String,Object>();
        map7.put("search","false");
        map7.put("minWidth","0px");
        map7.put("cRemarks","");
        map7.put("prop","zb");
        map7.put("label","运费/销售额");
        list.add(map7);

        object.put("column",list);
        return object;
    }
    /**
     * 品项数分析_Jane
     */
    @BaseServiceDescribe(moduleName = "报表",btnName = "品项数分析")
    public Page<Map<String,Object>> seachPxsfx(ShipmentOrderFx fx)throws ServiceException{
        PageHelper.startPage(fx.getPage(),fx.getRows());
        Page<Map<String,Object>> list= (Page<Map<String,Object>>)dao.seachPxsfx(fx);
        for(Map<String,Object> orderFx:list.getResult()){

            if("0.0".equals(orderFx.get("yf").toString())){
                orderFx.put("zb",0);
            }else{
                if("0.0000".equals(orderFx.get("xse").toString())) {
                    orderFx.put("zb", 0);
                }else{
                    if("0.00".equals(orderFx.get("xse").toString())) {
                        orderFx.put("zb", 0);
                    }else {
                        String f = orderFx.get("xse").toString().replaceAll(",", "");
                        BigDecimal f1 = new BigDecimal(Double.parseDouble(orderFx.get("yf").toString()) / Double.parseDouble(f)).setScale(2, BigDecimal.ROUND_HALF_UP);
                        orderFx.put("zb", f1);
                    }
                }
            }
        }
        return list;
    }
    /**
     * 导出 品项数分析表头
     *
     */
    @BaseServiceDescribe(moduleName = "导出报表表头",btnName = "导出品项数分析表头")
    public List<ExcelExportEntity> seachPxsfxExporttitle(ShipmentOrderFx fx){
        List<ExcelExportEntity> list=new ArrayList<>();
        if(fx.getZlids()!=null&&fx.getZlids().size()>0) {
            list.add(new ExcelExportEntity("货品种类", "zlmc"));
        }
        if(fx.getHpids()!=null&&fx.getHpids().size()>0){
        list.add(new ExcelExportEntity("货品名称","hpmc"));
        }
        if(fx.getChanneIds()!=null&&fx.getChanneIds().size()>0) {
            list.add(new ExcelExportEntity("销售渠道", "qdmc"));
        }
        list.add(new ExcelExportEntity("销售数量","xssl"));
        list.add(new ExcelExportEntity("单价","je"));
        list.add(new ExcelExportEntity("销售额","xse"));
       list.add(new ExcelExportEntity("运费", "yf"));
       list.add(new ExcelExportEntity("运费/销售额","zb"));
        return list;
    }
    /**
     *导出 品项数分析_Jane
     */
    @BaseServiceDescribe(moduleName = "导出报表",btnName = "导出品项数分析")
    public List<Map<String,Object>> seachPxsfxExport(ShipmentOrderFx fx){
        List<Map<String,Object>> list= dao.seachPxsfx(fx);
        for(Map<String,Object> orderFx:list){
            if("0.0".equals(orderFx.get("yf").toString())){
                orderFx.put("zb",0);
            }else{
                if("0.0000".equals(orderFx.get("xse").toString())) {
                    orderFx.put("zb", 0);
                }else {
                    if("0.00".equals(orderFx.get("xse").toString())) {
                        orderFx.put("zb", 0);
                    }else {
                        String f = orderFx.get("xse").toString().replaceAll(",", "");
                        BigDecimal f1 = new BigDecimal(Double.parseDouble(orderFx.get("yf").toString()) / Double.parseDouble(f)).setScale(2, BigDecimal.ROUND_HALF_UP);
                        orderFx.put("zb", f1.toString());
                    }
                }
            }
        }
        return list;
    }
    /**
     * 求最大公约数,计算占比
     */
    public Integer GreatestCommonDivisor(String yf,String xse){
        int ed=(int)Double.parseDouble(xse+0.5);
        int yfa=(int)Double.parseDouble(yf+0.5);
        int remainder=ed%yfa;
        while (remainder!=0){
            ed=yfa;
            yfa=remainder;
            remainder=ed%yfa;
        }
        return yfa;
    }
    /**
     * 计算运费
     */
    public Double yfjs(String hpid,ShipmentOrderFx fx){
        Double zyf=0.00;
        List<ShipmentOrderFx> hpzl=dao.dghpzl(fx,hpid);
        for(ShipmentOrderFx ofx:hpzl){
            ShipmentOrderFx sozl=dao.sozzl(ofx.getSoid());
            if(!"0.00".equals(sozl.getYf())){
                Double zb=0.0;
                if(0.0!=sozl.getZzl()) {
                     zb = ofx.getZzl() / sozl.getZzl();
                }
               zyf+=Double.parseDouble(new DecimalFormat("#.00").format(zb*Double.parseDouble(sozl.getYf())));
            }
        }

        return zyf;
    }
    /**
     *查询当前货主下的所有货品种类_Jane
     */
    @BaseServiceDescribe(moduleName = "报表",btnName = "查询当前货主下的所有货品种类")
    public List<WmsGoodsType> seachHpzl(String ckid,String hzid){
        return dao.seachHpzl(ckid,hzid);
    }
    /**
     *根据种类Id查询货品_Jane
     */
    @BaseServiceDescribe(moduleName = "报表",btnName = "根据种类Id查询货品")
    public List<WmsGoods> seachHpByZlid(List<String> list){
        return  dao.seachHpByZlid(list);
    }

    /**
     * 查询订单渠道_Jane
     * @param ckid
     * @param hzid
     * @return
     */
//    @BaseServiceDescribe(moduleName = "报表",btnName = "查询订单渠道")
//    public List<WmsOrderChannel> seachDdqd(String ckid,String hzid){
//        return dao.seachDdqd(ckid,hzid,);
//    }
    /**
     * 物流快递表_Jane
     * @param ckid
     * @param hzid
     * @return
     */
    @BaseServiceDescribe(moduleName = "报表",btnName = "查询物流快递报表")
    public Page<LogisticsExpress> seachWlkd(String ckid,String hzid,LogisticsExpress entity){
        PageHelper.startPage(entity.getPage(),entity.getRows());
        Page<LogisticsExpress> fx=(Page<LogisticsExpress>)dao.seachWlkd(ckid,hzid,entity);
        for(LogisticsExpress orderFx:fx.getResult()){
            orderFx.setCphz(dao.cphz(orderFx.getYsdh()));
            orderFx.setYsxs(dao.ysxs(orderFx.getYsdh()));
            String value = "";
            value = sysDictionaryService.getTypeName(DictionaryInfo.wms_shipment_order_cyslb.toString(), orderFx.getCyslb());
            orderFx.setShow_cyslb(value);
            if(!"".equals(orderFx.getDdqdid())&&!"0".equals(orderFx.getDdqdid())){
               WmsOrderChannel ddqd=dao.seachDdqd(ckid,hzid,orderFx.getDdqdid());
               if("0".equals(ddqd.getFjd())){
                   orderFx.setYiji(ddqd.getMc());
               }else{
                   WmsOrderChannel ddqd2=dao.seachDdqd(ckid,hzid,ddqd.getFjd());
                   if("0".equals(ddqd2.getFjd())){
                       orderFx.setYiji(ddqd2.getMc());
                       orderFx.setErji(ddqd.getMc());
                   }else{
                       WmsOrderChannel ddqd3=dao.seachDdqd(ckid,hzid,ddqd2.getFjd());
                       if("0".equals(ddqd3.getFjd())){
                           orderFx.setYiji(ddqd3.getMc());
                           orderFx.setErji(ddqd2.getMc());
                           orderFx.setSanji(ddqd.getMc());
                       }
                   }
               }
            }
        }
        return fx;
    }
    /**
     * 物流快递表_Jane
     * @param ckid
     * @param hzid
     * @return
     */
    @BaseServiceDescribe(moduleName = "导出报表",btnName = "导出物流快递报表")
    public List<LogisticsExpress> seachWlkdExport(String ckid,String hzid,LogisticsExpress entity){
        List<LogisticsExpress> fx=dao.seachWlkd(ckid,hzid,entity);
        for(LogisticsExpress orderFx:fx){
            orderFx.setCphz(dao.cphz(orderFx.getYsdh()));
            orderFx.setYsxs(dao.ysxs(orderFx.getYsdh()));
            if(!"".equals(orderFx.getDdqdid())){
                WmsOrderChannel ddqd=dao.seachDdqd(ckid,hzid,orderFx.getDdqdid());
                if(ddqd==null){
                    System.out.println(orderFx.getDdqdid());
                }
                System.out.println(ddqd.getFjd());
                if("0".equals(ddqd.getFjd())){
                    orderFx.setYiji(ddqd.getMc());
                }else{
                    WmsOrderChannel ddqd2=dao.seachDdqd(ckid,hzid,ddqd.getFjd());
                    if("0".equals(ddqd2.getFjd())){
                        orderFx.setYiji(ddqd2.getMc());
                        orderFx.setErji(ddqd.getMc());
                    }else{
                        WmsOrderChannel ddqd3=dao.seachDdqd(ckid,hzid,ddqd2.getFjd());
                        if("0".equals(ddqd3.getFjd())){
                            orderFx.setYiji(ddqd3.getMc());
                            orderFx.setErji(ddqd2.getMc());
                            orderFx.setSanji(ddqd.getMc());
                        }
                    }
                }
            }
        }
        return fx;
    }
}
